<template>
    <div class="app-container">
        <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch"
                 label-width="68px">
                        <el-form-item label="工单编号" prop="orderCode">
                            <el-input
                                    v-model="queryParams.orderCode"
                                    placeholder="请输入工单编号"
                                    clearable
                                    @keyup.enter.native="handleQuery"
                            />
                        </el-form-item>
                        <el-form-item label="工单类型" prop="orderType">
                            <el-select v-model="queryParams.orderType" placeholder="请选择工单类型" clearable>
                                <el-option
                                        v-for="dict in dict.type.order_type"
                                        :key="dict.value"
                                        :label="dict.label"
                                        :value="dict.value"
                                />
                            </el-select>
                        </el-form-item>
                        <el-form-item label="报单时间" prop="createTime">
                            <el-date-picker clearable
                                            v-model="queryParams.createTime"
                                            type="date"
                                            value-format="yyyy-MM-dd"
                                            placeholder="请选择报单时间">
                            </el-date-picker>
                        </el-form-item>
                        <el-form-item label="接单时间">
                            <el-date-picker
                                    v-model="daterangeReceiveTime"
                                    style="width: 240px"
                                    value-format="yyyy-MM-dd"
                                    type="daterange"
                                    range-separator="-"
                                    start-placeholder="开始日期"
                                    end-placeholder="结束日期"
                            ></el-date-picker>
                        </el-form-item>
                        <el-form-item label="单据状态" prop="orderStatus">
                            <el-select v-model="queryParams.orderStatus" placeholder="请选择单据状态" clearable>
                                <el-option
                                        v-for="dict in dict.type.order_status"
                                        :key="dict.value"
                                        :label="dict.label"
                                        :value="dict.value"
                                />
                            </el-select>
                        </el-form-item>
            <el-form-item>
                <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
                <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
            </el-form-item>
        </el-form>

        <el-row :gutter="10" class="mb8">
            <el-col :span="1.5">
                <el-button
                        type="primary"
                        plain
                        icon="el-icon-plus"
                        size="mini"
                        @click="handleAdd"
                        v-hasPermi="['business:order:add']"
                >新增
                </el-button>
            </el-col>
<!--            <el-col :span="1.5">-->
<!--                <el-button-->
<!--                        type="success"-->
<!--                        plain-->
<!--                        icon="el-icon-edit"-->
<!--                        size="mini"-->
<!--                        :disabled="single"-->
<!--                        @click="handleUpdate"-->
<!--                        v-hasPermi="['business:order:edit']"-->
<!--                >修改-->
<!--                </el-button>-->
<!--            </el-col>-->
<!--            <el-col :span="1.5">-->
<!--                <el-button-->
<!--                        type="danger"-->
<!--                        plain-->
<!--                        icon="el-icon-delete"-->
<!--                        size="mini"-->
<!--                        :disabled="multiple"-->
<!--                        @click="handleDelete"-->
<!--                        v-hasPermi="['business:order:remove']"-->
<!--                >删除-->
<!--                </el-button>-->
<!--            </el-col>-->
<!--            <el-col :span="1.5">-->
<!--                <el-button-->
<!--                        type="warning"-->
<!--                        plain-->
<!--                        icon="el-icon-download"-->
<!--                        size="mini"-->
<!--                        @click="handleExport"-->
<!--                        v-hasPermi="['business:order:export']"-->
<!--                >导出-->
<!--                </el-button>-->
<!--            </el-col>-->
            <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
        </el-row>

        <el-table v-loading="loading" :data="orderList" @selection-change="handleSelectionChange">
            <el-table-column type="selection" width="55" align="center"/>
                    <el-table-column label="自增主键" align="center" prop="id"/>
                    <el-table-column label="工单编号" align="center" prop="orderCode"/>
                    <el-table-column label="工单类型" align="center" prop="orderType">
                        <template slot-scope="scope">
                                <dict-tag :options="dict.type.order_type" :value="scope.row.orderType"/>
                        </template>
                    </el-table-column>
                    <el-table-column label="工单描述" align="center" prop="orderDesc"/>
                    <el-table-column label="维修地点" align="center" prop="location"/>
                    <el-table-column label="预约时间" align="center" prop="bookingTime" width="180">
                        <template slot-scope="scope">
                            <span>{{ parseTime(scope.row.bookingTime, '{y}-{m}-{d}') }}</span>
                        </template>
                    </el-table-column>
                    <el-table-column label="接单人" align="center" prop="receiveBy"/>
                    <el-table-column label="接单时间" align="center" prop="receiveTime" width="180">
                        <template slot-scope="scope">
                            <span>{{ parseTime(scope.row.receiveTime, '{y}-{m}-{d}') }}</span>
                        </template>
                    </el-table-column>
                    <el-table-column label="完工时间" align="center" prop="completeTime" width="180">
                        <template slot-scope="scope">
                            <span>{{ parseTime(scope.row.completeTime, '{y}-{m}-{d}') }}</span>
                        </template>
                    </el-table-column>
                    <el-table-column label="单据状态" align="center" prop="orderStatus">
                        <template slot-scope="scope">
                                <dict-tag :options="dict.type.order_status" :value="scope.row.orderStatus"/>
                        </template>
                    </el-table-column>
            <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
                <template slot-scope="scope">
                    <el-button
                            size="mini"
                            type="text"
                            icon="el-icon-edit"
                            @click="handleUpdate(scope.row)"
                            v-hasPermi="['business:order:edit']"
                    >修改
                    </el-button>
                  <el-button
                    size="mini"
                    type="text"
                    icon="el-icon-edit"
                    @click="handleQueryOne(scope.row)"
                    v-hasPermi="['business:order:edit']"
                  >查看
                  </el-button>
                    <el-button
                            size="mini"
                            type="text"
                            icon="el-icon-delete"
                            @click="handleDelete(scope.row)"
                            v-hasPermi="['business:order:remove']"
                    >删除
                    </el-button>
                </template>
            </el-table-column>
        </el-table>

        <pagination
                v-show="total>0"
                :total="total"
                :page.sync="queryParams.pageNum"
                :limit.sync="queryParams.pageSize"
                @pagination="getList"
        />

        <!-- 添加或修改工单管理对话框 -->
        <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
            <el-form ref="form" :model="form" :rules="rules" label-width="80px">
                                <el-form-item label="工单类型" prop="orderType">
                                    <el-select v-model="form.orderType" placeholder="请选择工单类型">
                                        <el-option
                                                v-for="dict in dict.type.order_type"
                                                :key="dict.value"
                                                :label="dict.label"
:value="dict.value"
                                        ></el-option>
                                    </el-select>
                                </el-form-item>
                                <el-form-item label="工单描述" prop="orderDesc">
                                    <el-input v-model="form.orderDesc" placeholder="请输入工单描述"/>
                                </el-form-item>
                                <el-form-item label="预约时间" prop="bookingTime">
                                    <el-date-picker clearable
                                                    v-model="form.bookingTime"
                                                    type="datetime"
                                                    value-format="yyyy-MM-dd HH:mm:ss"
                                                    placeholder="请选择预约时间">
                                    </el-date-picker>
                                </el-form-item>
              <el-form-item label="维修地点" prop="location">
                <el-input v-model="form.location" placeholder="请输入维修地点"/>
              </el-form-item>
            </el-form>
            <div slot="footer" class="dialog-footer">
                <el-button type="primary" @click="submitForm">确 定</el-button>
                <el-button @click="cancel">取 消</el-button>
            </div>
        </el-dialog>
      <!-- 查看 -->
      <el-dialog :title="title" :visible.sync="queryopen" width="500px" append-to-body>
        <el-form ref="form" :model="form" :rules="rules" label-width="80px">
          <el-form-item label="工单编号" prop="orderCode">
            <el-input v-model="form.orderCode" readonly placeholder="请输入工单编号"/>
          </el-form-item>
          <el-form-item label="工单类型" prop="orderType">
            <el-select v-model="form.orderType" placeholder="请选择工单类型" disabled>
              <el-option
                v-for="dict in dict.type.order_type"
                :key="dict.value"
                :label="dict.label"
                :value="dict.value"
              ></el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="工单描述" prop="orderDesc">
            <el-input v-model="form.orderDesc" readonly placeholder="请输入工单描述"/>
          </el-form-item>
<!--          <el-form-item label="报单图片">-->
<!--            <image-upload disabled v-model="form.submitPic"/>-->
<!--          </el-form-item>-->
          <el-form-item label="预约时间" prop="bookingTime">
            <el-date-picker clearable readonly
                            v-model="form.bookingTime"
                            type="datetime"
                            value-format="yyyy-MM-dd HH:mm:ss"
                            placeholder="请选择预约时间">
            </el-date-picker>
          </el-form-item>
          <el-form-item label="维修地点" prop="orderDesc">
            <el-input v-model="form.location" readonly />
          </el-form-item>
          <el-form-item label="是否派单" prop="isSend">
            <el-select v-model="form.isSend" placeholder="请选择是否派单" disabled>
              <el-option readonly
                v-for="dict in dict.type.is_send"
                :key="dict.value"
                :label="dict.label"
                :value="dict.value"
              ></el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="派单人" prop="sendBy">
            <el-input v-model="form.sendBy" readonly placeholder="请输入派单人"/>
          </el-form-item>
          <el-form-item label="派单时间" prop="sendTime">
            <el-date-picker clearable readonly
                            v-model="form.sendTime"
                            type="date"
                            value-format="yyyy-MM-dd"
                            placeholder="请选择派单时间">
            </el-date-picker>
          </el-form-item>
          <el-form-item label="接单人" prop="receiveBy">
            <el-input v-model="form.receiveBy" readonly placeholder="请输入接单人"/>
          </el-form-item>
          <el-form-item label="接单时间" prop="receiveTime">
            <el-date-picker clearable readonly
                            v-model="form.receiveTime"
                            type="date"
                            value-format="yyyy-MM-dd"
                            placeholder="请选择接单时间">
            </el-date-picker>
          </el-form-item>
          <el-form-item label="完工时间" prop="completeTime">
            <el-date-picker clearable readonly
                            v-model="form.completeTime"
                            type="date"
                            value-format="yyyy-MM-dd"
                            placeholder="请选择完工时间">
            </el-date-picker>
          </el-form-item>
<!--          <el-form-item label="完工图片" disabled="true" aria-readonly="true">-->
<!--            <image-upload v-model="form.completePic" readonly disabled="true"/>-->
<!--          </el-form-item>-->
          <el-form-item label="评价打分" prop="evaluateScore">
            <el-select v-model="form.evaluateScore" placeholder="请选择评价打分" disabled>
              <el-option
                v-for="dict in dict.type.evaluate_score"
                :key="dict.value"
                :label="dict.label"
                :value="dict.value"
              ></el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="评价内容" prop="evaluateContent">
            <el-input v-model="form.evaluateContent" readonly type="textarea" placeholder="请输入内容"/>
          </el-form-item>
          <el-form-item label="评价时间" prop="evaluateTime">
            <el-date-picker clearable readonly
                            v-model="form.evaluateTime"
                            type="date"
                            value-format="yyyy-MM-dd"
                            placeholder="请选择评价时间">
            </el-date-picker>
          </el-form-item>
          <el-form-item label="取消原因" prop="cancelReason">
            <el-input v-model="form.cancelReason" readonly placeholder="请输入取消原因"/>
          </el-form-item>
          <el-form-item label="取消人" prop="cancelBy">
            <el-input v-model="form.cancelBy" readonly placeholder="请输入取消人"/>
          </el-form-item>
          <el-form-item label="取消时间" prop="cancelTime">
            <el-date-picker clearable readonly
                            v-model="form.cancelTime"
                            type="date"
                            value-format="yyyy-MM-dd"
                            placeholder="请选择取消时间">
            </el-date-picker>
          </el-form-item>
          <el-form-item label="单据状态" prop="orderStatus">
            <el-select v-model="form.orderStatus" disabled placeholder="请选择单据状态">
              <el-option
                v-for="dict in dict.type.order_status"
                :key="dict.value"
                :label="dict.label"
                :value="dict.value"
              ></el-option>
            </el-select>
          </el-form-item>
        </el-form>
      </el-dialog>
    </div>
</template>

<script>
    import {
        listOrder,
        getOrder,
        delOrder,
        addOrder,
        updateOrder,
        getLocation
    } from "@/api/business/order";

    export default {
        name: "Order",
            dicts: ['is_send', 'order_status', 'evaluate_score', 'order_type'],
        data() {
            return {
                // 遮罩层
                loading: true,
                defaultLocation: null,
                // 选中数组
                ids: [],
                // 非单个禁用
                single: true,
                // 非多个禁用
                multiple: true,
                // 显示搜索条件
                showSearch: true,
                // 总条数
                total: 0,
                // 工单管理表格数据
                    orderList: [],
                // 弹出层标题
                title: "",
                // 是否显示弹出层
                open: false,
                queryopen: false,
                        // 单据状态时间范围
                        daterangeBookingTime: [],
                        // 单据状态时间范围
                        daterangeReceiveTime: [],
                // 查询参数
                queryParams: {
                    pageNum: 1,
                    pageSize: 10,
                                orderCode: null,
                                orderType: null,
                                createTime: null,
                                receiveTime: null,
                                orderStatus: null
                },
                // 表单参数
                form: {},
                // 表单校验
                rules: {
                                orderType: [
                                {
                                    required: true, message: "工单类型不能为空", trigger: "change" }
                            ],
                                orderDesc: [
                                {
                                    required: true, message: "工单描述不能为空", trigger: "blur" }
                            ],
                                bookingTime: [
                                {
                                    required: true, message: "预约时间不能为空", trigger: "blur" }
                            ],
                                location: [
                                  {
                                    required: true, message: "维修地点不能为空", trigger: "blur" }
                                ],
                }
            };
        },
        created() {
            this.getList();
        },
        methods: {
            /** 查询工单管理列表 */
            getList() {
                this.loading = true;
                        this.queryParams.params = {};
                        if (null != this.daterangeBookingTime && '' != this.daterangeBookingTime) {
                            this.queryParams.params["beginBookingTime"] = this.daterangeBookingTime[0];
                            this.queryParams.params["endBookingTime"] = this.daterangeBookingTime[1];
                        }
                        if (null != this.daterangeReceiveTime && '' != this.daterangeReceiveTime) {
                            this.queryParams.params["beginReceiveTime"] = this.daterangeReceiveTime[0];
                            this.queryParams.params["endReceiveTime"] = this.daterangeReceiveTime[1];
                        }
                listOrder(this.queryParams).then(response => {
                    this.orderList = response.rows;
                    this.total = response.total;
                    this.loading = false;
                });
            },
            // 取消按钮
            cancel() {
                this.open = false;
                this.reset();
            },
            // 表单重置
            reset() {
                this.form = {
                                id: null,
                                orderCode: null,
                                orderType: null,
                                orderDesc: null,
                                location: null,
                                submitPic: null,
                                bookingTime: null,
                                isSend: null,
                                createBy: null,
                                createTime: null,
                                sendBy: null,
                                sendTime: null,
                                receiveBy: null,
                                receiveTime: null,
                                completeTime: null,
                                completePic: null,
                                evaluateScore: null,
                                evaluateContent: null,
                                evaluateTime: null,
                                cancelReason: null,
                                cancelBy: null,
                                cancelTime: null,
                                orderStatus: null
                };
                this.resetForm("form");
            },
            /** 搜索按钮操作 */
            handleQuery() {
                this.queryParams.pageNum = 1;
                this.getList();
            },
            /** 重置按钮操作 */
            resetQuery() {
                        this.daterangeBookingTime = [];
                        this.daterangeReceiveTime = [];
                this.resetForm("queryForm");
                this.handleQuery();
            },
            // 多选框选中数据
            handleSelectionChange(selection) {
                this.ids = selection.map(item => item.id)
                this.single = selection.length !== 1
                this.multiple = !selection.length
            },
            /** 新增按钮操作 */
            handleAdd() {
                this.reset();
                this.open = true;
                this.title = "添加工单管理";
                //获取登录人默认地址
                getLocation().then(response => {
                  this.form.location = response.location
                })
            },
            /** 修改按钮操作 */
            handleUpdate(row) {
                this.reset();
                const id = row.id || this.ids
                getOrder(id).then(response => {
                    this.form = response.data;
                    this.open = true;
                    this.title = "修改工单管理";
                });
            },
            handleQueryOne(row) {
              this.reset();
              const id = row.id || this.ids
              getOrder(id).then(response => {
                this.form = response.data;
                this.queryopen = true;
                this.title = "查看工单管理";
              });
            },
            /** 提交按钮 */
            submitForm() {
              this.$refs['form'].validate(valid => {
                if (valid) {
                  // this.form.organizationId = this.form.value[0]
                  if (this.form.id !== null) {
                    updateOrder(this.form).then(response => {
                      this.$modal.msgSuccess('修改成功')
                      this.open = false
                      this.getList()
                    })
                  } else {
                    addOrder(this.form).then(response => {
                      this.$modal.msgSuccess('新增成功')
                      this.open = false
                      this.getList()
                    })
                  }
                }
              })
            },
            /** 删除按钮操作 */
            handleDelete(row) {
              const id = row.id || this.ids
              this.$confirm('是否确认删除该订单?', '警告', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning'
              }).then(function() {
                return delOrder(id)
              }).then(() => {
                this.getList()
                this.msgSuccess('删除成功')
              })
            },
    /** 导出按钮操作 */
    handleExport()
    {
        this.download('business/order/export', {
            ...this.queryParams
        }, `order_${new Date().getTime()}.xlsx`)
    }
    }
    }
    ;
</script>
